package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.app.MainApp;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.config.Constants;
import com.zzh.takeoutapp.utils.DataCleanManager;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.SPUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/22.
 */

public class SettingActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.data_size)
    TextView data_size;
    @Bind(R.id.submit)
    TextView submit;
    @Bind(R.id.clear_cache)
    RelativeLayout clear_cache;
    private LoadingUtil dialog;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "设置");
        File file = new File(MainApp.getContext().getCacheDir().getPath());
        try {
            data_size.setText(DataCleanManager.getCacheSize(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0x01:
                    dialog.stopShowLoading();
                    data_size.setText("0.0KB");
                    break;
                case 0x02:
                    dialog.stopShowLoading();
                    break;
            }
        }
    };

    @OnClick({R.id.clear_cache, R.id.submit})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.clear_cache:
                Message msg = new Message();
                dialog = new LoadingUtil(this);
                dialog.setDialogTitle("清理中...");
                dialog.setCanCancel(false);
                dialog.startShowLoading();
                try {
                    DataCleanManager.cleanApplicationData(MainApp.getContext());
                    msg.what = 0x01;
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.what = 0x02;
                }
                handler.sendMessageDelayed(msg, 1000);
                break;
            case R.id.submit:
                SPUtils.removeData(this, Constants.TOKEN);
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setType("MineFragment");
                start_Activity(intent);
                finish_Activity();
                break;
        }

    }
}
